package com.cljz.makepassenger.ui.activity;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.cljz.makepassenger.R;
import com.cljz.makepassenger.base.App;
import com.cljz.makepassenger.base.BaseActivity;
import com.cljz.makepassenger.present.activity.SettingPwdP;
import com.cljz.makepassenger.utils.ActivityManager;
import com.cljz.makepassenger.widget.CodeEditText;
import com.jakewharton.rxbinding.view.RxView;
import com.jakewharton.rxbinding.widget.RxTextView;

import butterknife.BindView;
import cn.droidlover.xdroidmvp.router.Router;
import rx.functions.Action1;

public class SettingPwdActivity extends BaseActivity<SettingPwdP> {

    //设置密码
    public static final int TYPE_SET = 1;
    //修改前确认密码
    public static final int TYPE_VERIFY = 2;
    //输入密码
    public static final int TYPE_INPUT = 3;
    //修改密码
    public static final int TYPE_MODIFY = 4;
    //重置密码
    public static final int TYPE_RESET = 5;

    @BindView(R.id.iv_back)
    ImageView ivBack;
    @BindView(R.id.tv_title_name)
    TextView tvTitleName;
    @BindView(R.id.cet_code)
    CodeEditText codeEdit;
    @BindView(R.id.tv_forget)
    TextView tvForget;
    @BindView(R.id.tv_text)
    TextView tvText;
    private int type;
    private String verifyCode;
    private String phone;

    @Override
    public void initData(Bundle savedInstanceState) {
        tvTitleName.setText("输入密码");
        type = getIntent().getIntExtra("type", TYPE_SET);
        switch (type) {
            case TYPE_SET:
                tvText.setText("请设置新密码");
                tvForget.setVisibility(View.GONE);
                break;
            case TYPE_VERIFY:
                tvText.setText("输入旧密码，以验证身份");
                tvForget.setVisibility(View.VISIBLE);
                break;
            case TYPE_INPUT:
                tvText.setText("请输入支付密码");
                tvForget.setVisibility(View.VISIBLE);
                break;
            case TYPE_MODIFY:
                tvText.setText("请设置新密码");
                tvForget.setVisibility(View.GONE);
                break;
            case TYPE_RESET:
                Bundle bundle = getIntent().getExtras();
                phone = bundle.getString("phone");
                verifyCode = bundle.getString("verify_code");
                tvText.setText("请输入新密码");
                tvForget.setVisibility(View.GONE);
        }
    }

    @Override
    public int getLayoutId() {
        return R.layout.activity_setting_pwd;
    }

    @Override
    public SettingPwdP newP() {
        return new SettingPwdP();
    }

    @Override
    protected void initListener() {
        RxView.clicks(ivBack)
                .compose(this.<Void>bindToLifecycle())
                .subscribe(new Action1<Void>() {
                    @Override
                    public void call(Void aVoid) {
                        finish();
                    }
                });
        RxView.clicks(tvForget)
                .compose(this.<Void>bindToLifecycle())
                .subscribe(new Action1<Void>() {
                    @Override
                    public void call(Void aVoid) {
                        Router.newIntent((Activity) App.getInstance())
                                .to(VerifyIdentityActivity.class)
                                .launch();
                    }
                });
        RxTextView.textChanges(codeEdit)
                .compose(this.<CharSequence>bindToLifecycle())
                .subscribe(new Action1<CharSequence>() {
                    @Override
                    public void call(CharSequence charSequence) {
                        if (charSequence.length() == 6) {
                            showLoadingDialog();
                            switch (type) {
                                case TYPE_SET:
                                    getP().setPwd(charSequence.toString());
                                    break;

                                case TYPE_VERIFY:
                                    getP().checkPwd(charSequence.toString());
                                    break;

                                case TYPE_INPUT:
                                    getP().checkPwd(charSequence.toString());
                                    break;

                                case TYPE_MODIFY:
                                    getP().modifyPws(charSequence.toString());
                                    break;
                                case TYPE_RESET:
                                    getP().resetPaymentPass(phone, verifyCode, charSequence.toString());
                            }
                        }
                    }
                });
    }

    public void setSuccess() {
        dismissLoadingDialog();
        showToast("设置成功",R.mipmap.nearby_success);
        finish();
    }

    public void modifySuccess() {
        dismissLoadingDialog();
        showToast("修改成功",R.mipmap.nearby_success);
        finish();
    }

    public void checkSuccess() {
        dismissLoadingDialog();
        if (type == TYPE_VERIFY) {
            Router.newIntent(SettingPwdActivity.this).putInt("type", TYPE_MODIFY).to(SettingPwdActivity.class).launch();
            finish();
        } else {
            Router.newIntent(SettingPwdActivity.this).to(WithdrawActivity.class).launch();
            finish();
        }
    }

    public void requestFailed(String error) {
        dismissLoadingDialog();
        codeEdit.setText("");
        showToast(error,R.mipmap.jinshi);
    }

    public void resetSuccess() {
        dismissLoadingDialog();
        showToast("重置成功",R.mipmap.nearby_success);
        ActivityManager.getInstance().finishAllActivity(SettingPwdActivity.class);
    }
}
